package com.olddriver.crm.web.controller;

import com.olddriver.crm.annotation.PermissionName;
import com.olddriver.crm.domain.TotalInfo;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.TotalInfoQueryObject;
import com.olddriver.crm.service.ITotalInfoService;
import jxl.Workbook;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("totalInfo")
public class TotalInfoController {
    @Autowired
    private ITotalInfoService totalInfoService;


    @RequiresPermissions("totalInfo:view")
    @PermissionName("查看事故信息页面")
    @RequestMapping("view")
    public String list() {
        return "AccidentTotalInfo/view";
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<TotalInfo> listAll() {
        return totalInfoService.listAll();
    }

    @ResponseBody
    @RequestMapping("query")
    @RequiresPermissions("totalInfo:query")
    @PermissionName("事故信息列表")
    public PageResult query(TotalInfoQueryObject qo) {
        PageResult result = totalInfoService.query(qo);
        return result;
    }

    //导出
    @RequestMapping("exportFile")
    public void exportFile(HttpServletResponse response) throws Exception{
        //文件下载响应头
        response.setHeader("Content-Disposition","attachment;filename=totalInfo.xls");
        //创建xls文件Excel
        WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
        //创建工作簿
        WritableSheet sheet = workbook.createSheet("day01", 0);
        WritableSheet sheet1 = workbook.createSheet("day02", 1);

        //设置标题
        sheet.addCell(new Label(0,0,"车主信息"));
        sheet.addCell(new Label(1,0,"保单号"));
        sheet.addCell(new Label(2,0,"保险机构"));
        sheet.addCell(new Label(3,0,"保险类型"));
        sheet.addCell(new Label(4,0,"事故地点"));
        sheet.addCell(new Label(5,0,"事故时间"));
        sheet.addCell(new Label(6,0,"处理员工"));
        sheet.addCell(new Label(7,0,"车辆信息"));
        //添加内容
        List<TotalInfo> totalInfos = totalInfoService.listAll();
        for (int i = 0,j = 1; i < totalInfos.size(); i++,j++){
            TotalInfo totalInfo= totalInfos.get(i);
            sheet.addCell(new Label(0,j,totalInfo.getOwner()));
            sheet.addCell(new Label(1,j,totalInfo.getPolicySn()));
            sheet.addCell(new Label(2,j,totalInfo.getOrganization()));
            sheet.addCell(new Label(3,j,totalInfo.getInsuranceType()));
            sheet.addCell(new Label(4,j,totalInfo.getAccidentPlace()));
            sheet.addCell(new DateTime(5,j,totalInfo.getReportTime()));//数据库没有时间会报错
            sheet.addCell(new Label(6,j,totalInfo.getEmployee()));
            sheet.addCell(new Label(7,j,totalInfo.getCarType()));
        }
        //写入数据
        workbook.write();
        //关闭资源
        workbook.close();
    }

}
